class Storage {
  getItem(key) {}
  setItem(key, value) {}
  removeItem(key) {}
  clearAll() {}
}

class LocalStorage extends Storage {
  getItem(key) {
    let value = localStorage.getItem(key)
    if (/^\{.*\}$/.test(value) || /^\[.*\]$/.test(value)) value = JSON.parse(value)
    return value
  }
  setItem(key, value) {
    if (typeof value === typeof {}) value = JSON.stringify(value)
    return localStorage.setItem(key, value)
  }
  removeItem(key) {
    return localStorage.removeItem(key)
  }
  clearAll() {
    localStorage.clear()
  }
}

class SessionStorage extends Storage {
  getItem(key) {
    let value = sesstionStorage.getItem(key)
    if (/^\{.*\}$/.test(value) || /^\[.*\]$/.test(value)) value = JSON.parse(value)
    return value
  }
  setItem(key, value) {
    if (typeof value === typeof {}) value = JSON.stringify(value)
    return sesstionStorage.setItem(key, value)
  }
  removeItem(key) {
    return sesstionStorage.removeItem(key)
  }
  clearAll() {
    sesstionStorage.clear()
  }
}

let storageType = 'localStorage' // 该值可以来源于配置项

let storage
if (storageType === 'localStorage') {
  storage = new LocalStorage()
} else if (storageType === 'sessionStorage') {
  storage = new SessionStorage()
}

export default storage
